package com.vibeplayer.app.model;

import java.io.File;

/**
 * 文件模型类，用于文件浏览器中表示文件或文件夹
 */
public class FileModel {
    private File file;
    private boolean isDirectory;
    private boolean isSelected;
    private String name;
    private String displayName;
    private String path;
    private long size;
    private long lastModified;
    
    public FileModel(File file) {
        this(file, file.getName());
    }
    
    public FileModel(File file, String displayName) {
        this.file = file;
        this.isDirectory = file.isDirectory();
        this.isSelected = false;
        this.name = file.getName();
        this.displayName = displayName;
        this.path = file.getAbsolutePath();
        this.size = file.length();
        this.lastModified = file.lastModified();
    }
    
    public File getFile() {
        return file;
    }
    
    public boolean isDirectory() {
        return isDirectory;
    }
    
    public boolean isSelected() {
        return isSelected;
    }
    
    public void setSelected(boolean selected) {
        isSelected = selected;
    }
    
    public String getName() {
        return displayName;
    }
    
    public String getActualName() {
        return name;
    }
    
    public String getPath() {
        return path;
    }
    
    public long getSize() {
        return size;
    }
    
    public long getLastModified() {
        return lastModified;
    }
    
    /**
     * 获取格式化的文件大小字符串
     * @return 格式化的文件大小字符串（如：1.5 MB）
     */
    public String getFormattedSize() {
        if (isDirectory) {
            return "";
        }
        
        final long KB = 1024;
        final long MB = KB * 1024;
        final long GB = MB * 1024;
        
        if (size < KB) {
            return size + " B";
        } else if (size < MB) {
            return String.format("%.1f KB", (float) size / KB);
        } else if (size < GB) {
            return String.format("%.1f MB", (float) size / MB);
        } else {
            return String.format("%.1f GB", (float) size / GB);
        }
    }
    
    /**
     * 获取文件扩展名
     * @return 文件扩展名（如：mp3），如果没有扩展名或是目录则返回空字符串
     */
    public String getExtension() {
        if (isDirectory || name.lastIndexOf(".") == -1) {
            return "";
        }
        return name.substring(name.lastIndexOf(".") + 1).toLowerCase();
    }
    
    /**
     * 判断是否是音频文件
     * @return 如果是音频文件返回true，否则返回false
     */
    public boolean isAudioFile() {
        if (isDirectory) {
            return false;
        }
        
        String ext = getExtension();
        return ext.equals("mp3") || ext.equals("wav") || ext.equals("ogg") || 
               ext.equals("flac") || ext.equals("aac") || ext.equals("m4a");
    }
} 